package gomz.server;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author Schatzmann
 * Diese Klasse wird verwendet um das GUI des Servers zu erstellen
 *
 * *****NOTIZEN:************
 * 
 */
public class GUIServer extends JFrame {
// ********************************************************************************
// Variablen
// ********************************************************************************
	private JLabel lblPort;
	private JTextField txtPort;
        private JLabel lblDBUser;
	private JTextField txtDBUser;
        private JLabel lblDBPW;
	private JTextField txtDBPW;
	private JButton btnStart;
        private JButton btnStop;
        
        private JLabel lblExportPath;
	private JTextField txtExportPath;
	private JButton btnSearch;
        
	private JTextArea txtLog;
        private MServer model;
// ********************************************************************************
// Konstruktor
// ********************************************************************************
        /**
         * Konstruktor welcher das GUI erzeugt
         * @param model model des MVC welches übergeben wird um den LogListener hinzuzufuegen
         */
	public GUIServer(MServer model) {
                this.model = model;
		this.setTitle("Message server");
		//Set Icon
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(GUIServer.class.getResource("/gomz/server/logo.png")));
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		
                //TopBox enthält: PORT / DBUser und DBPassword und ButtonStart / Stopp
		Box topBox = Box.createHorizontalBox();
                //DownBox enthält: Logger Export Pfad und Durchsuchen Button
                Box downBox = Box.createHorizontalBox();
		pane.add(topBox, BorderLayout.NORTH);
                pane.add(downBox, BorderLayout.SOUTH);

		lblPort = new JLabel("Port");
		topBox.add(lblPort);
		txtPort = new JTextField("123");
		txtPort.setPreferredSize(new Dimension(150, 21));
		topBox.add(txtPort);
                txtPort.setEditable(false);
                
                lblDBUser = new JLabel("DB User:");
                topBox.add(lblDBUser);
                
                txtDBUser = new JTextField("root");
		txtDBUser.setPreferredSize(new Dimension(150, 21));
                topBox.add(txtDBUser);
                
                lblDBPW = new JLabel("DB Password:");
                topBox.add(lblDBPW);
                
                txtDBPW = new JTextField("");
		txtDBPW.setPreferredSize(new Dimension(150, 21));
                topBox.add(txtDBPW);
                
           
		btnStart = new JButton("Start");
		topBox.add(btnStart);
                
                btnStop = new JButton("Stop");
		topBox.add(btnStop);

                lblExportPath = new JLabel("Log Export Pfad:");
                downBox.add(lblExportPath);
                
                txtExportPath = new JTextField("");
		txtExportPath.setPreferredSize(new Dimension(150, 21));
                downBox.add(txtExportPath);
                
                btnSearch = new JButton("Durchsuchen...");
		downBox.add(btnSearch);
                
		
		JScrollPane paneContent = new JScrollPane();
		paneContent.setPreferredSize(new Dimension(300, 300));
		txtLog = new JTextArea();
		txtLog.setFont(new Font("Courier New", 0, 12));
		paneContent.setViewportView(txtLog);
		pane.add(paneContent, BorderLayout.CENTER);
		
                //Listener wenn das Fenster geschlossen wird, beende alles
                addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent evt) {
                                exitForm(evt);
                        }
                });
                
                //Wenn TxtData veraendert dann fuege es auch im Log Hinzu
                model.addLogListener(new LogListener() {
                        public void doNotifyTxtDataChange(String logString) {
                                txtLog.append(logString + "\n");
                        }
                });

		pack();
		setVisible(true);
	}
// ********************************************************************************
// Getter/Setter
// ********************************************************************************
        public JButton getBtnStart() {
            return btnStart;
        }

        public void setBtnGo(JButton btnGo) {
            this.btnStart = btnGo;
        }

        public JTextField getTxtPort() {
            return txtPort;
        }

        public void setTxtPort(JTextField txtPort) {
            this.txtPort = txtPort;
        }

        public JTextField getTxtDBUser() {
            return txtDBUser;
        }

        public void setTxtDBUser(JTextField txtDBUser) {
            this.txtDBUser = txtDBUser;
        }

        public JTextField getTxtDBPW() {
            return txtDBPW;
        }

        public void setTxtDBPW(JTextField txtDBPW) {
            this.txtDBPW = txtDBPW;
        }

        public  JTextArea getTxtLog() {
            return txtLog;
        }

        public void setTxtLog(JTextArea txtLog) {
            this.txtLog = txtLog;
        }
        
        public void setTxtLog(String text) {
            this.txtLog.append(text);
        }

        public MServer getModel() {
        return model;
        }

        public void setModel(MServer model) {
            this.model = model;
        }

        public JTextField getTxtExportPath() {
            return txtExportPath;
        }

        public void setTxtExportPath(JTextField txtExportPath) {
            this.txtExportPath = txtExportPath;
        }

        public JButton getBtnSearch() {
            return btnSearch;
        }

        public void setBtnSearch(JButton btnSearch) {
            this.btnSearch = btnSearch;
        }

        public JButton getBtnStop() {
            return btnStop;
        }

        public void setBtnStop(JButton btnStop) {
            this.btnStop = btnStop;
        }
        
        //beende programm
	private void exitForm(WindowEvent evt) {
		System.exit(0);
	}
        
        
}
